﻿using System.Linq;

namespace Estimate
{
    public class Db
    {
        readonly EstimateDataContext context;
        public Db(EstimateDataContext context)
        {
            this.context = context;
        }

        public Firm GetFirmOwn() { return context.Firms.Where(x => x.FirmTypeID == 1).First(); }

        public decimal Dollar
        {
            get { return context.Rates.Where(x => x.RateID == 8).Select(x => x.RateValue).First(); }
        }

        #region InvoiceIncome
        public void DeleteInvoiceIncome(InvoiceIncome invoiceIncome)
        {
            while (invoiceIncome.InvoiceIncomeToHistories.Count > 0) DeleteInvoiceIncomeToHistory(invoiceIncome.InvoiceIncomeToHistories.First());
            if (invoiceIncome.InvoiceRelocations.Count > 0)
            {
                InvoiceRelocation invoiceRelocation = invoiceIncome.InvoiceRelocations.First();
                context.InvoiceRelocations.DeleteOnSubmit(invoiceRelocation);
                context.SubmitChanges();
            }
        }

        public void DeleteInvoiceIncomeToHistory(InvoiceIncomeToHistory invoiceIncomeToHistory)
        {
            context.InvoiceIncomePrices.DeleteAllOnSubmit(invoiceIncomeToHistory.InvoiceIncomePrices);
            InvoiceRelocationToHistory invoiceRelocationToHistory = invoiceIncomeToHistory.InvoiceRelocationToHistories.First();
            ProductHistory historyRelocation = invoiceRelocationToHistory.ProductHistory;
            context.InvoiceRelocationToHistories.DeleteOnSubmit(invoiceRelocationToHistory);
            context.ProductHistories.DeleteOnSubmit(historyRelocation);

            ProductHistory historyIncome = invoiceIncomeToHistory.ProductHistory;
            context.InvoiceIncomeToHistories.DeleteOnSubmit(invoiceIncomeToHistory);
            
            context.ProductHistories.DeleteOnSubmit(historyIncome);

            context.SubmitChanges();
        }

        public InvoiceIncomeToHistory GetInvoiceIncomeToHistory(int invoiceIncomeToHistoryID)
        { return context.InvoiceIncomeToHistories.Where(x => x.InvoiceIncomeToHistoryID == invoiceIncomeToHistoryID).FirstOrDefault(); }

        #endregion

        #region InvoiceRelocation
        public void DeleteInvoiceRelocation(InvoiceRelocation invoiceRelocation)
        {
            while (invoiceRelocation.InvoiceRelocationToHistories.Count > 0)
                DeleteInvoiceRelocationToHistory(invoiceRelocation.InvoiceRelocationToHistories.First());

            if (invoiceRelocation.InvoiceWriteoffs.Count > 0)
            {
                InvoiceWriteoff invoiceWritteOff = invoiceRelocation.InvoiceWriteoffs.First();
                context.InvoiceWriteoffs.DeleteOnSubmit(invoiceWritteOff);
                context.SubmitChanges();
            }
        }

        public void DeleteInvoiceRelocationToHistory(InvoiceRelocationToHistory invoiceRelocationToHistory)
        {
            InvoiceWriteoffToHistory invoiceWriteoffToHistory = invoiceRelocationToHistory.InvoiceWriteoffToHistories.First();
            ProductHistory historyWriteoff = invoiceWriteoffToHistory.ProductHistory;
            context.InvoiceWriteoffToHistories.DeleteOnSubmit(invoiceWriteoffToHistory);
            context.ProductHistories.DeleteOnSubmit(historyWriteoff);
            ProductHistory historyRelocation = invoiceRelocationToHistory.ProductHistory;
            context.InvoiceRelocationToHistories.DeleteOnSubmit(invoiceRelocationToHistory);
            context.ProductHistories.DeleteOnSubmit(historyRelocation);
            context.SubmitChanges();
        }

        public InvoiceRelocationToHistory GetInvoiceRelocationToHistory(int invoiceRelocationToHistoryID)
        { return context.InvoiceRelocationToHistories.Where(x => x.InvoiceRelocationToHistoryID == invoiceRelocationToHistoryID).FirstOrDefault(); }
        #endregion

        #region InvoiceWriteoff

        internal void DeleteInvoiceWriteoff(InvoiceWriteoff invoiceWriteoff)
        {
            while(invoiceWriteoff.InvoiceWriteoffToHistories.Count>0)
                DeleteInvoiceWriteoffToHistory(invoiceWriteoff.InvoiceWriteoffToHistories.First( ));
            context.InvoiceWriteoffs.DeleteOnSubmit(invoiceWriteoff);
            context.SubmitChanges();
        }

        internal void DeleteInvoiceWriteoffToHistory(InvoiceWriteoffToHistory invoiceWriteoffToHistory)
        {
            context.ProductHistories.DeleteOnSubmit(invoiceWriteoffToHistory.ProductHistory);
            context.InvoiceWriteoffToHistories.DeleteOnSubmit(invoiceWriteoffToHistory);
            context.SubmitChanges();
        }

        internal InvoiceWriteoffToHistory GetInvoiceWriteoffToHistory(int invoiceWriteoffToHistoryID)
        { return context.InvoiceWriteoffToHistories.Where(x => x.InvoiceWriteoffToHistoryID == invoiceWriteoffToHistoryID).First(); }
        #endregion

        #region InvoiceOutcome
        public void DeleteInvoiceOutcome(InvoiceOutcome invoiceOutcome)
        {
            while (invoiceOutcome.InvoiceOutcomeToHistories.Count > 0)
                DeleteInvoiceOutcomeToHistory(invoiceOutcome.InvoiceOutcomeToHistories.First());

            InvoiceWriteoff invoiceWritteOff = invoiceOutcome.InvoiceWriteoff;
            context.InvoiceOutcomes.DeleteOnSubmit(invoiceOutcome);
            if (invoiceWritteOff != null)
                context.InvoiceWriteoffs.DeleteOnSubmit(invoiceWritteOff);
            context.InvoiceOutcomes.DeleteOnSubmit(invoiceOutcome);
            context.SubmitChanges();
        }

        public void DeleteInvoiceOutcomeToHistory(InvoiceOutcomeToHistory invoiceOutcomeToHistory)
        {
            context.ProductHistories.DeleteOnSubmit(invoiceOutcomeToHistory.ProductHistory);
            context.InvoiceOutcomeToHistories.DeleteOnSubmit(invoiceOutcomeToHistory);

            InvoiceWriteoffToHistory invoiceWriteoffToHistory = invoiceOutcomeToHistory.InvoiceWriteoffToHistory;
            if (invoiceWriteoffToHistory != null) DeleteInvoiceWriteoffToHistory(invoiceWriteoffToHistory);
   
            context.SubmitChanges();
        }





        public InvoiceOutcomeToHistory GetInvoiceOutcomeToHistory(int invoiceOutcomeToHistoryID)
        {
         return context.InvoiceOutcomeToHistories.Where(x => x.InvoiceOutcomeToHistoryID
             == invoiceOutcomeToHistoryID).FirstOrDefault();
     }
        #endregion
    
    }


}
